package com.monkeyboy.utils;

import com.monkeyboy.modul.HtmlContent;
import org.jsoup.Jsoup;

import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.util.StringUtils;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

/**
 * @Author Gavin
 * @date 2020.09.01 11:07
 */
public class HtmlParseUtil {
    public static void main(String[] args) throws Exception {
    }

    /*
    爬取jd的数据
     */
    public static ArrayList<HtmlContent> JD_Search(String keyWords) throws Exception {
        ArrayList<HtmlContent> objects = new ArrayList<>();
        //获取请求
        String url = "https://search.jd.com/Search?keyword=" + keyWords;

        Document document = Jsoup.connect(url).timeout(10000).get();
        //所有再js中可以使用的方法都可以使用
        Element j_goodsList = document.getElementById("J_goodsList");
        //获取所有的li元素
        Elements lis = j_goodsList.getElementsByTag("li");
        //获取元素中的内容
        for (Element pre : lis) {
            String img = pre.getElementsByTag("img").eq(0).attr("src");
            String price = pre.getElementsByClass("p-price").eq(0).text();
            String title = pre.getElementsByClass("p-name").eq(0).text();
            if (StringUtils.isEmpty(img) || StringUtils.isEmpty(price) || StringUtils.isEmpty(title)) {
                continue;
            }
            System.out.println("=======================");
            System.out.println(img);
            System.out.println(price);
            System.out.println(title);
            HtmlContent preLi = new HtmlContent();
            preLi.setImg(img);
            preLi.setPrice(price);
            preLi.setTitle(title);
            objects.add(preLi);
        }
        return objects;
    }

    public static ArrayList<HtmlContent> scrapAihuishouData() throws Exception {
        ArrayList<HtmlContent> objects = new ArrayList<>();
        //获取请求
        String url = "https://www.aihuishou.com/shouji/b52";

        Document document = Jsoup.connect(url).timeout(10000).get();
        //所有再js中可以使用的方法都可以使用
        Elements div = document.getElementsByClass("product-list-wrapper");
        System.out.println(div.html());
        //获取所有的li元素
        Elements lis = div.get(0).getElementsByTag("li");
        //获取元素中的内容
        for (Element pre : lis) {
            String img = pre.getElementsByTag("a").get(0).getElementsByTag("img").attr("src");
            String price = pre.getElementsByTag("a").get(0).getElementsByClass("price").text();
            String title = pre.getElementsByTag("a").get(0).getElementsByTag("p").eq(0).text();
            if (StringUtils.isEmpty(img) || StringUtils.isEmpty(price) || StringUtils.isEmpty(title)) {
                continue;
            }
            System.out.println("=======================");
            System.out.println(img);
            System.out.println(price);
            System.out.println(title);
            HtmlContent preLi = new HtmlContent();
            preLi.setImg(img);
            preLi.setPrice(price);
            preLi.setTitle(title);
            objects.add(preLi);
        }
        return objects;
    }
}
